package main

import (
	"fmt"
	"go.manyji.com/lib/app"
	"go.manyji.com/lib/net/socks5"
	"go.manyji.com/sieve/pf"
	"io"
	"net/http"
	"net/url"
	"strconv"
	"strings"
	"time"
)

func main() {
	app.Start()
	pf.Run("linkedin", query)
	app.Block()
}

func query(account string) (bool, error) {
	pp := pf.GetProxyPool("")
	proxy := pp.Get()
	if proxy == nil {
		return false, fmt.Errorf("no available proxy")
	}
	req := &Req{Account: account, data: map[string]string{}, proxy: proxy}
	defer func() {
		if req.proxy != nil { // step内判断proxy不可用就置为空，不返还
			pp.Put(req.proxy)
		}
	}()
	if err := req.Step1(); err != nil {
		return false, err
	}
	fmt.Println(req.data)
	if err := req.Step2(); err != nil {
		return false, err
	}
	fmt.Println(req.data)
	if err := req.Step3(); err != nil {
		return false, err
	}
	fmt.Println(req.data)
	return req.Step4()
}

type Req struct {
	Account string
	data    map[string]string
	proxy   *socks5.Proxy
}

func (r *Req) Step1() error {
	resp, err := pf.Http(r.proxy).Get("https://www.linkedin.com/login?fromSignIn=true&trk=guest_homepage-basic_nav-header-signin", func(req *http.Request) {
		addHeader(req)
	})
	if err != nil {
		return err
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		return fmt.Errorf("StatusCode %d", resp.StatusCode)
	}
	for _, c := range resp.Cookies() {
		r.data[c.Name] = c.Value
	}
	b, err := io.ReadAll(resp.Body)
	if err != nil {
		return err
	}
	// fmt.Println(1, string(b))
	inputs := findAllInputHidden(string(b))
	keys := []string{"csrfToken", "resendUrl", "session_redirect", "parentPageKey", "pageInstance", "controlId", "trk", "authUUID", "encrypted_session_key", "ac", "sIdString", "loginCsrfParam", "fp_data", "_d", "showGoogleOneTapLogin", "apfc"}
	for _, k := range keys {
		r.data[k] = inputs[k]
	}
	return nil
}

/*
apfc如何生成？貌似不影响请求
apfc={"df":{"a":"v7mkkaaT24SzCrbXNEXi7w==","b":"KRzkLUp0A/kts8yNdtKl1Nbyn+PkKFVI7IyuNyAn/QgCUP6H7g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cgZwT0a02m7EJGtLsC99oy7y+3/3anM4gTKiVtm+l6GVscM6ahvQLKJHSraxBbop+EvOKUJD8FG3Ry3IA8g/VjL0tvN5LCOEnMILyi/SqIA==","d":0,"e":2}}
*/

func (r *Req) Step2() error {
	u := "https://platform.linkedin.com/litms/utag/checkpoint-frontend/utag.js?cb=" + strconv.FormatInt(time.Now().UnixMilli(), 10)
	resp, err := pf.Http(r.proxy).Get(u, func(req *http.Request) {
		addHeader(req)
		req.AddCookie(&http.Cookie{Name: "lang", Value: r.data["lang"]})
		req.AddCookie(&http.Cookie{Name: "bcookie", Value: r.data["bcookie"]})
		req.AddCookie(&http.Cookie{Name: "lidc", Value: r.data["lidc"]})
	})
	if err != nil {
		return err
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		return fmt.Errorf("StatusCode %d", resp.StatusCode)
	}
	b, err := io.ReadAll(resp.Body)
	if err != nil {
		return err
	}
	// fmt.Println(2, string(b))
	s := string(b)
	i := strings.Index(s, "AMCV_")
	// j := strings.Index(s, "AMCVS_")
	r.data["AMCV"] = s[i+5 : i+40]
	return nil
}

func (r *Req) Step3() error {
	u := "https://dpm.demdex.net/id?d_visid_ver=5.1.1&d_fieldgroup=MC&d_rtbd=json&d_ver=2&d_orgid=" + r.data["AMCV"] + "&d_nsid=0&ts=" + strconv.FormatInt(time.Now().UnixMilli(), 10)
	resp, err := pf.Http(r.proxy).Get(u, func(req *http.Request) {
		addHeader(req)
		req.Header.Set("Origin", "https://www.linkedin.com")
		req.Header.Set("Referer", "https://www.linkedin.com/")
	})
	if err != nil {
		return err
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		return fmt.Errorf("StatusCode %d", resp.StatusCode)
	}
	// b, err := io.ReadAll(resp.Body)
	// if err != nil {
	// 	return err
	// }
	// fmt.Println(3, string(b))
	for _, c := range resp.Cookies() {
		r.data[c.Name] = c.Value
	}
	return nil
}

func (r *Req) Step4() (bool, error) {
	u := "https://www.linkedin.com/checkpoint/lg/login-submit"
	if n, _ := strconv.Atoi(r.Account); n > 0 {
		r.Account = "+" + r.Account
	}
	data := url.Values{}
	data.Set("csrfToken", r.data["JSESSIONID"])
	data.Set("session_key", r.Account)
	data.Set("ac", r.data["ac"])
	data.Set("sIdString", r.data["sIdString"])
	data.Set("parentPageKey", r.data["parentPageKey"])
	data.Set("pageInstance", r.data["pageInstance"])
	data.Set("trk", r.data["trk"])
	data.Set("authUUID", r.data["authUUID"])
	data.Set("session_redirect", r.data["session_redirect"])
	data.Set("loginCsrfParam", r.data["loginCsrfParam"])
	data.Set("apfc", r.data["apfc"])
	data.Set("fp_data", "default")
	data.Set("_d", "d")
	data.Set("showGoogleOneTapLogin", "true")
	data.Set("controlId", "d_checkpoint_lg_consumerLogin-login_submit_button")
	data.Set("session_password", "12312312321313")
	resp, err := pf.Http(r.proxy).Post(u, data,
		func(req *http.Request) {
			addHeader(req)
			req.Header.Set("Origin", "https://www.linkedin.com")
			req.Header.Set("Referer", "https://www.linkedin.com/checkpoint/lg/login-submit")

			req.AddCookie(&http.Cookie{Name: "li_rm", Value: r.data["li_rm"]})
			req.AddCookie(&http.Cookie{Name: "JSESSIONID", Value: r.data["JSESSIONID"]})
			req.AddCookie(&http.Cookie{Name: "bcookie", Value: r.data["bcookie"]})
			req.AddCookie(&http.Cookie{Name: "bscookie", Value: r.data["bscookie"]})
			req.AddCookie(&http.Cookie{Name: "lidc", Value: r.data["lidc"]})
			req.AddCookie(&http.Cookie{Name: "aam_uuid", Value: r.data["demdex"]})
		})
	if err != nil {
		return false, err
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		return false, fmt.Errorf("StatusCode %d", resp.StatusCode)
	}
	b, err := io.ReadAll(resp.Body)
	if err != nil {
		return false, err
	}
	// fmt.Println(4, string(b))
	return !strings.Contains(string(b), "Couldn"), nil
	// return !strings.Contains(string(b), "login_error_create_account"), nil
}

func addHeader(req *http.Request) {
	req.Header.Set("sec-ch-ua", `"Google Chrome";v="111", "Not(A:Brand";v="8", "Chromium";v="111"`)
	req.Header.Set("sec-ch-ua-mobile", `?0`)
	req.Header.Set("sec-ch-ua-platform", `"Windows"`)
	req.Header.Set("Sec-Fetch-Dest", `document`)
	req.Header.Set("Sec-Fetch-Mode", `navigate`)
	req.Header.Set("Sec-Fetch-Site", `none`)
	req.Header.Set("Sec-Fetch-User", `?1`)
	req.Header.Set("Upgrade-Insecure-Requests", `1`)
	req.Header.Set("User-Agent", `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36`)
	req.Header.Set("Keep-Alive", `600`)
}

func findAllInputHidden(html string) map[string]string {
	m := map[string]string{}
	for {
		i := strings.Index(html, `<input type="hidden"`)
		if i < 0 {
			break
		}
		html = html[i:]
		i = strings.Index(html, `>`)
		if i < 0 {
			break
		}
		input := html[:i]
		html = html[i+1:]

		if name, value := findKV(input); name != "" {
			m[name] = value
		}
	}
	return m
}

func findKV(input string) (name string, value string) {
	for _, s := range strings.Fields(strings.Trim(input, `</>`)) {
		if strings.HasPrefix(s, `name="`) {
			name = strings.Trim(s[5:], `"`)
		}
		if strings.HasPrefix(s, `value="`) {
			value = strings.Trim(s[6:], `"`)
		}
	}
	return
}
